package com.hsc.www.amqp.config;


import com.rabbitmq.client.Channel;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * 描述：封装sender
 *
 * @author fw
 * 创建时间：2017年10月14日 下午10:30:00
 * @version 1.0.0
 */

@Component
@Aspect
public class TransactionReceiver {
    Logger logger = LoggerFactory.getLogger(TransactionReceiver.class);


    @Autowired
    MessageTransientStore messageTransientStore;

    @Autowired
    MessageDurableStore messageDurableStore;

    /**
     * 定义注解类型的切点，只要方法上有该注解，都会匹配
     */
    @Pointcut("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
    public void point() {

    }

    @Around("point()")
    public void sendMsg(ProceedingJoinPoint joinPoint) throws Throwable {
        Channel channel = null;
        Message message = null;
        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof Channel) {
                channel = (Channel) arg;
            } else if (arg instanceof Message) {
                message = (Message) arg;
            }
        }
        if (channel == null || message == null) {
            logger.error("被@RabbitListener修饰的方法 必须有Channel、Message参数");
        }

//        ((byte[])message.getPayload())




        try {
            joinPoint.proceed();
        } catch (Exception e) {
            throw e;
        }


        System.out.println("dddd");


//
//        RabbitMetaMessage message = new RabbitMetaMessage();
//        message.setMessageId(UUID.randomUUID().toString());
//        message.setExchange(transMessage.exchange());
//        message.setRoutingKey(transMessage.routingkey());
//        message.setBizName(transMessage.bizName());
//        message.setPrepareSendTime(new Date());
//
//        int payloadIndex = transMessage.payloadIndex();
//        if (payloadIndex > joinPoint.getArgs().length - 1 || payloadIndex < -1) {
//            throw new RuntimeException("payloadIndex  错误！");
//        }
//
//        /**发送前暂存消息,这里payload暂存的是所有方法参数*/
//        message.setPayload(joinPoint.getArgs());//
//        messageTransientStore.saveMessage(message);
//
//        /** 执行业务函数 */
//        try {
//            if (payloadIndex == -1) {
//                message.setPayload(returnObj);
//            } else {
//                message.setPayload(joinPoint.getArgs()[payloadIndex]);
//            }
//        } catch (Exception e) {
//            messageTransientStore.removeMessage(message.getMessageId());
//            throw e;
//        }


    }

}
